package com.sinosoft.cloud.configuration;

import com.sinosoft.cloud.annotation.ReqJsonBean;
import com.sinosoft.cloud.annotation.ReqMsgBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.util.Map;

/**
 * 描述:
 *
 * @author 张旭东
 * @date 2018/11/27
 */
@SpringBootConfiguration
public class AutoTestConfiguration {
    @Autowired
    private ConfigurableListableBeanFactory beanFactory;

    @Bean
    public JsonMakerContainer getJsonMakerContainer() {
        System.out.println("初始化json组装");
        Map<String, Object> jsonMakerBeans = beanFactory.getBeansWithAnnotation(ReqMsgBody.class);
        JsonMakerContainer jsonMakerContainer = JsonMakerContainer.getInstance();

        for (String beanName : jsonMakerBeans.keySet()) {
            ReqMsgBody reqMsgBody = beanFactory.findAnnotationOnBean(beanName, ReqMsgBody.class);
            jsonMakerContainer.addJsonBean(reqMsgBody.channelType(), reqMsgBody.businessType(), (ReqJsonBean) jsonMakerBeans.get(beanName));
        }
        return jsonMakerContainer;
    }
}
